﻿using MaterialDesignThemes.Wpf;
using RPAVIEW.util;
using RPAVIEW.views.controlLibrary;
using SUNRPA.CONTROLS.IMPL.domain.flowItemParam;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace RPAVIEW.views.EditPage.flows.AddExcelFilterN {
    /// <summary>
    /// Input.xaml 的交互逻辑
    /// </summary>
    public partial class Input:BaseParamViewAction{
        public Input() {
            InitializeComponent();
            AddExcelFilterNParam param = ParamUtil.GetCurrentParam<AddExcelFilterNParam>();
            List<string> cons = param.Conditions;
            if(cons != null) {
                foreach(string con in cons) {
                    string[] nv = con.Split(new string[] {"==="}, StringSplitOptions.RemoveEmptyEntries);
                    StackPanel conPanel = GetCon();
                    TextBox nameText = conPanel.Children[0] as TextBox;
                    TextBox valueText = conPanel.Children[2] as TextBox;
                    nameText.Text = nv[0];
                    valueText.Text = nv[1];
                    this.conditions.Children.Add(conPanel);
                }
            }
        }

        public override void UpdateSource() {
            this.excelObject.GetBindingExpression(SelVarTextBox.text).UpdateSource();
            this.filterType.GetBindingExpression(ComboBox.SelectedIndexProperty).UpdateSource();
            this.sheetName.GetBindingExpression(SelVarTextBox.text).UpdateSource();
            AddExcelFilterNParam param = ParamUtil.GetCurrentParam<AddExcelFilterNParam>();
            List<string> cons = new List<string>();
            foreach(StackPanel child in this.conditions.Children) { 
                SelVarTextBox nameText = child.Children[0] as SelVarTextBox;
                SelVarTextBox valueText = child.Children[2] as SelVarTextBox;
                string name = nameText.Text;
                string value = valueText.Text;
                cons.Add(name + "===" + value);
            }
            param.Conditions = cons;
        }

        private void FilterTypeChanged(object sender, SelectionChangedEventArgs e) {
            int filterType = this.filterType.SelectedIndex;
            if(filterType == 0) { 
                ElementUtil.ShowRow(this.conditionRow);
            } else { 
                ElementUtil.HideRow(this.conditionRow);
            }
        }

        private void AddCondition(object sender, RoutedEventArgs e) {
            StackPanel con = this.GetCon();
            this.conditions.Children.Add(con);
        }

        private void RemoveCondition(object sender, RoutedEventArgs e) {
            Button button = sender as Button;
            this.conditions.Children.Remove(button.Parent as StackPanel);
        }

        private StackPanel GetCon() { 
            StackPanel con = new StackPanel();
            con.Orientation = Orientation.Horizontal;

            SelVarTextBox sheetText = new SelVarTextBox();
            SelVarTextBox valueText = new SelVarTextBox();
            Label label = new Label();
            con.Children.Add(sheetText);
            con.Children.Add(label);
            con.Children.Add(valueText);

            sheetText.Padding = new Thickness(1, 4, 1,1);
            sheetText.Width = 142;
            sheetText.Hint = "列名：从1开始(也可以是列名如A)-n代表导数第n列";

            valueText.Padding = new Thickness(1, 3, 1,1);
            valueText.Width = 285;
            valueText.Hint = "筛选值";

            label.Content = " = ";
            label.Margin = new Thickness(0, 20, 0,0);
            label.Style = (Style)this.FindResource("LableFontStyle");

            Button button = new Button();
            button.Width = 30;
            button.Height = 20;
            button.Style = (Style)this.FindResource("MaterialDesignRaisedLightButton");
            button.ToolTip = "删除条件";
            button.HorizontalAlignment = HorizontalAlignment.Left;
            button.VerticalAlignment = VerticalAlignment.Bottom;
            button.Margin = new Thickness(5, 0, 0,5);
            button.Padding = new Thickness(0, 0, 0,0);
            button.Click += new RoutedEventHandler(RemoveCondition);
            con.Children.Add(button);

            PackIcon icon = new PackIcon();
            icon.Kind = PackIconKind.Minus;
            button.Content = icon;
            return con;
        }
    }
}
